
local juying = fk.CreateSkill{
  name = "lingling__juying",
}

Fk:loadTranslationTable{
  ["lingling__juying"] = "聚英",
  [":lingling__juying"] = "每名角色限一次，当一名角色横置、翻至背面、或成为延时锦囊牌的目标时，你可以失去1点体力取消之。"..
  "当一名你对其如此做过的其他角色使用【杀】时，其须交给你一张牌，否则无效。",

  ["#lingling__juying1-invoke"] = "聚英：是否失去1点体力，防止 %dest 被横置？",
  ["#lingling__juying2-invoke"] = "聚英：是否失去1点体力，防止 %dest 被翻面？",
  ["#lingling__juying3-invoke"] = "聚英：是否失去1点体力，防止 %dest 成为%arg的目标？",
  ["#lingling__juying-give"] = "聚英：你需交给 %src 一张牌，否则此【杀】无效",
}

juying:addEffect(fk.BeforeChainStateChange, {
  anim_type = "support",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(juying.name) and
      not target.chained and not target.dead and player.hp > 0 and
      not table.contains(player:getTableMark(juying.name), target.id)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    if room:askToSkillInvoke(player, {
      skill_name = juying.name,
      prompt = "#lingling__juying1-invoke::"..target.id,
    }) then
      event:setCostData(self, {tos = {target}})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:addTableMark(player, juying.name, target.id)
    data.prevented = true
    room:loseHp(player, 1, juying.name)
  end,
})

juying:addEffect(fk.BeforeTurnOver, {
  anim_type = "support",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(juying.name) and
      target.faceup and not target.dead and player.hp > 0 and
      not table.contains(player:getTableMark(juying.name), target.id)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    if room:askToSkillInvoke(player, {
      skill_name = juying.name,
      prompt = "#lingling__juying2-invoke::"..target.id,
    }) then
      event:setCostData(self, {tos = {target}})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:addTableMark(player, juying.name, target.id)
    data.prevented = true
    room:loseHp(player, 1, juying.name)
  end,
})

juying:addEffect(fk.TargetConfirming, {
  anim_type = "support",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(juying.name) and
      data.card.sub_type == Card.SubtypeDelayedTrick and not target.dead and player.hp > 0 and
      not table.contains(player:getTableMark(juying.name), target.id)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    if room:askToSkillInvoke(player, {
      skill_name = juying.name,
      prompt = "#lingling__juying3-invoke::"..target.id..":"..data.card:toLogString(),
    }) then
      event:setCostData(self, {tos = {target}})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:addTableMark(player, juying.name, target.id)
    data:cancelTarget(target)
    room:loseHp(player, 1, juying.name)
  end,
})

juying:addEffect(fk.CardUsing, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    return target ~= player and player:hasSkill(juying.name) and
      data.card.trueName == "slash" and table.contains(player:getTableMark(juying.name), target.id)
  end,
  on_cost = function (self, event, target, player, data)
    event:setCostData(self, {tos = {target}})
    return true
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if not target.dead then
      local card = room:askToCards(target, {
        min_num = 1,
        max_num = 1,
        include_equip = true,
        skill_name = juying.name,
        prompt = "#lingling__juying-give:"..player.id,
        cancelable = true,
      })
      if #card > 0 then
        room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonGive, juying.name, nil, false, target)
        return
      end
    end
    data:removeAllTargets()
  end,
})

return juying
